package cn.com.homeyoda.aimate.aihealth.ui.login;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.LOGIN_SUCCESS;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.lifecycle.ViewModelProvider;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityLoginBinding;

/**
 * Created By 刘纯贵
 * Created Time 2020/4/30
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {

    private ImageView ivQrcode;
    private LoginViewModel viewModel;

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(starter);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel; // 如果没绑定 ViewModel，可以返回 0 或不使用
    }

    @Override
    public LoginViewModel initViewModel() {
        viewModel = new ViewModelProvider(this, AppViewModelFactory.getInstance(getApplication()))
                .get(LoginViewModel.class);
        return viewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.qrCodeData.observe(this, bitmap -> {
            if (bitmap != null) {
                ivQrcode.setImageBitmap(bitmap);
            }
        });

        LiveEventBus.get(LOGIN_SUCCESS, String.class).observe(this, userId -> {
            doLogin(userId);
            finish();
        });
    }

    @Override
    public void initData() {
        super.initData();
        showLoginDialog(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.getLoginQrCode();
    }

    @Override
    protected void onPause() {
        super.onPause();
        viewModel.stopPolling();
        finish();
    }

    private void showLoginDialog(Bitmap bitmap) {
        MaterialDialog loginDialog = new MaterialDialog.Builder(this)
                .customView(R.layout.dialog_qrcode_login, false)
                .show();
        ivQrcode = loginDialog.getView().findViewById(R.id.iv_wechat_login_qrcode);
        loginDialog.getView().findViewById(R.id.iv_close).setOnClickListener(v -> finish());
        if (loginDialog.getWindow() != null) {
            loginDialog.setCancelable(false);
            loginDialog.setCanceledOnTouchOutside(false);
            loginDialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
            loginDialog.getWindow().setLayout(600, WRAP_CONTENT);
            loginDialog.getWindow().setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(0));
        }
    }

    @Override
    public void onBackPressed() {
        // do nothing
    }

}
